//
//  CS_QueryPanel_SingleSelectView.m
//  CSUIKit
//
//  Created by 浙大网新中研软件 on 2017/11/10.
//

#import "CS_QueryPanel_SingleSelectView.h"

@interface CS_QueryPanel_SingleSelectView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) UITableView *rightTableView;

@end

@implementation CS_QueryPanel_SingleSelectView

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    if([self isLeveledSingleSelectView]){
        //分级的单项选择的处理
        return [self layoutLeveledSingleSelectView:superView primaryY:primaryY];
    }
    
    //未分级的单项选择的处理
    CGFloat lcoalPrimaryY = 0.0;
    
    for (int i = 0; i < self.conditionModel.selectItems.count; i++) {
        
        id selectObject = [self.conditionModel.selectItems objectAtIndex:i];
        
        CSSelectableModel *selectableModel;
        
        if ([selectObject isKindOfClass:[NSString class]]) {
            
            selectableModel = [CSSelectableModel new];
            selectableModel.code = (NSString *)selectObject;
            selectableModel.content = (NSString *)selectObject;
            
        }else if([selectObject isKindOfClass:[CSSelectableModel class]]){
            selectableModel = [self.conditionModel.selectItems objectAtIndex:i];
        }
        
        __block CSSelectableModel *weakSelectModel = selectableModel;
        
        WS(weakSelf);
        
        CS_Page_CellButton *cellButton = [CS_Page_CellButton page_CellButtonWithCellTag:selectableModel layoutSubViewHandler:^CGFloat(UIView *superView, CGFloat rightMargin) {
            
            CGFloat innerPrimaryY = 17;
            
            CS_Page_TextView *page_TextView = [CS_Page_TextView page_textViewWithFont:[UIFont systemFontOfSize:FONT_SIZE_2] textColor:COLOR_TEXT_1 content:weakSelectModel.content
                                                                        textAlignment:NSTextAlignmentLeft];
            innerPrimaryY = [page_TextView layoutView:superView primaryY:innerPrimaryY];
            
            UIView *view = [[UIView alloc] init];
            view.frame = CGRectMake(0, innerPrimaryY + 2, kWindowW, 1);
            view.backgroundColor = COLOR_TOP_SEPERATOR_LINE;
            [superView addSubview:view];
            
            return innerPrimaryY + 5;
            
        } clickHander:^(id cellTag) {
            
            CSSelectableModel *selectableModel = cellTag;
            weakSelf.onConditionCongiureFinishedHandler(selectableModel.code, selectableModel.content);
            
        }];
        
        lcoalPrimaryY = [cellButton layoutView:self.scrollView primaryY:lcoalPrimaryY];
        
    }
    lcoalPrimaryY -= 4;
    
    self.scrollView.contentSize = CGSizeMake(0, lcoalPrimaryY);
    self.scrollView.frame = CGRectMake(0, 0, kWindowW, lcoalPrimaryY);
    if (lcoalPrimaryY > kWindowW - primaryY) {
        self.scrollView.frame = CGRectMake(0, 0, kWindowW, kWindowW - primaryY);
    }
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.scrollView];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, HEIGHT_REST_CONDITION_VIEW);
    
    self.maskButton.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    
    [self insertSubview:self.maskButton atIndex:0];
    
    [superView addSubview:self];
    
    return kWindowH;
}

-(CGFloat)layoutLeveledSingleSelectView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    //添加左侧按钮
    self.leftTableView.frame = CGRectMake(0, 0, kWindowW * 0.4, HEIGHT_REST_CONDITION_VIEW - HEIGHT_LEFT_FOR_MASK);
    self.rightTableView.frame = CGRectMake(kWindowW * 0.4, 0, kWindowW * 0.65, HEIGHT_REST_CONDITION_VIEW - HEIGHT_LEFT_FOR_MASK);
    [self addSubview:self.leftTableView];
    [self addSubview:self.rightTableView];
    
    //分隔线
    UIView *seperatorLineView = [[UIView alloc] init];
    seperatorLineView.frame = CGRectMake(kWindowW * 0.4, 0, 1, HEIGHT_REST_CONDITION_VIEW - HEIGHT_LEFT_FOR_MASK);
    seperatorLineView.backgroundColor = COLOR_TOP_SEPERATOR_LINE;
    [self addSubview:seperatorLineView];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, HEIGHT_REST_CONDITION_VIEW);
    
    //添加灰色取消区域按钮
    self.maskButton.frame = CGRectMake(0, kWindowH - HEIGHT_REST_CONDITION_VIEW, kWindowW, HEIGHT_REST_CONDITION_VIEW);
    
    [self insertSubview:self.maskButton atIndex:0];
    
    [superView addSubview:self];
    
    return kWindowH;
    
}

#pragma mark --- UIScrollViewDelegate, UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(tableView == self.leftTableView){
        
        return self.conditionModel.selectItems.count;
        
    }else if(tableView == self.rightTableView){
        
        NSInteger selectedIndex = self.leftTableView.indexPathForSelectedRow.row;
        return ((CSSelectableModel *)[self.conditionModel.selectItems objectAtIndex:selectedIndex]).subItems.count;
        
    }
    
    return 0;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(tableView == self.leftTableView){
        
        [self.rightTableView reloadData];
        
    }else if(tableView == self.rightTableView){
        
        NSInteger leftIndex = self.leftTableView.indexPathForSelectedRow.row;
        CSSelectableModel *selectableModel = [((CSSelectableModel *)[self.conditionModel.selectItems objectAtIndex:leftIndex]).subItems objectAtIndex:indexPath.row];
        
        self.onConditionCongiureFinishedHandler(selectableModel.code, selectableModel.content);
        
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(tableView == self.leftTableView){
        
        CSSelectableModel *selectableModel = [self.conditionModel.selectItems objectAtIndex:indexPath.row];
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell"];
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"leftCell"];
        }
        cell.textLabel.font = [UIFont systemFontOfSize:FONT_SIZE_3];
        cell.textLabel.textColor = COLOR_TEXT_1;
        cell.textLabel.text = selectableModel.content;
        cell.imageView.image = selectableModel.image;
        
        return cell;
        
    }else if(tableView == self.rightTableView){
        
        NSInteger leftIndex = self.leftTableView.indexPathForSelectedRow.row;
        CSSelectableModel *selectableModel = [((CSSelectableModel *)[self.conditionModel.selectItems objectAtIndex:leftIndex]).subItems objectAtIndex:indexPath.row];
       
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell"];
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rightCell"];
        }
        cell.textLabel.font = [UIFont systemFontOfSize:FONT_SIZE_3];
        cell.textLabel.textColor = COLOR_TEXT_1;
        cell.textLabel.text = selectableModel.content;
        cell.imageView.image = selectableModel.image;
        
        return cell;
        
    }
    
    return nil;
}



/**
 * 判断是否是分层级的单选
 */
-(BOOL)isLeveledSingleSelectView{
    
    for(int i = 0; i < self.conditionModel.selectItems.count; i++){
        
        CSSelectableModel *selectableModel = [self.conditionModel.selectItems objectAtIndex:i];
        if(selectableModel.subItems != nil && selectableModel.subItems.count > 0){
            return YES;
        }
        
    }
    return NO;
    
}

-(UIScrollView *)scrollView{
    
    if (_scrollView == nil) {
        _scrollView = [UIScrollView new];
        _scrollView.bounces = NO;
    }
    return _scrollView;
    
}

-(UITableView *)leftTableView{
    
    if(_leftTableView == nil){
        
        _leftTableView = [UITableView new];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        STYLE_TABLE_VIEW(_leftTableView);
        
    }
    return _leftTableView;
    
}

-(UITableView *)rightTableView{
    
    if(_rightTableView == nil){
        
        _rightTableView = [UITableView new];
        _rightTableView.dataSource = self;
        _rightTableView.delegate = self;
        STYLE_TABLE_VIEW(_rightTableView);
        
    }
    return _rightTableView;
    
}

@end
